/**
 * @class   ButlerImpl
 * @brief   
 *
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: ButlerImpl.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_ButlerImpl_H__
 #define __WOSH_Services_ButlerImpl_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Map.h>
 #include <core/Table.h>
 #include <core/Notification.h>
 #include <core/MessageQueue.h>

 #include <core/Communicator.h>
 #include <core/Interpreter.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/InterpreterService.h>
 #include <interfaces/services/Butler.h>

 #include <framework/communication/TextMessage.h>

 #include <interfaces/services/BuildingManager.h>
 #include <framework/building/DoorBell.h>

 #include "IButlerListener.h"


using namespace wosh;
using namespace wosh::communication;
using namespace wosh::building;

class DoorBellInfo {
	public:
		DoorBellInfo() {

		 }
		virtual ~DoorBellInfo() { }

	public:
		long getLastEvent() const {
			if ( this->history.rbegin() != this->history.rend() )
				this->history.rbegin()->first;
			return 0;
		 }

	public:
		std::string owner;
		std::string homename;
		URI uri;
		std::map<long, DoorBell::BELL_STATE> history;

};

class ButlerImpl : public BundleGenericWorker
 {
	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		ButlerImpl( BundleGeneric& bundle );
		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~ButlerImpl();


	public:
		void evalMessageIncoming( const Message& message );

		void evalResponse( const Message& message );

		void updateDoorBells();

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setDoorBellNotify( short value )						{ this->doorBellsNotify = value; }
		void setDoorBellPriority( Notification::PRIORITY value )	{ this->doorBellsPriority = value; }

		void setListener( IButlerListener* list )					{ this->listener = list; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const Map<std::string, DoorBellInfo*>& getDoorBells() const		{ return this->doorBells; }
		Map<std::string, DoorBellInfo*>& getDoorBells()					{ return this->doorBells; }

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		void evalDoorBells( const Table* childrenprops, const URI& uri );

	protected:
		void evalMessage( Message* message );

		void evalDoorBellEvent( DoorBellInfo* dBell, DoorBell::BELL_STATE state );
		void evalUsersAtHomeEvent( int count );

	protected:
		void runThread();

//@}

		MessageQueue msgQueue;				///< messages' queue
		IButlerListener* listener;

		Map<std::string, DoorBellInfo*> doorBells;
		short doorBellsNotify; // 0=disabled; 1=always; 2=when home is empty
		Notification::PRIORITY doorBellsPriority;


}; // class def


#endif //__WOSH_Services_ButlerImpl_H__
